package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.AdministrativeRegion;
import com.lxw.nonghe.service.AdministrativeRegionService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
/**
 * <p>
 * 行政区域控制器
 * </p>
 *
 * @author lxw
 * @since 2025-05-09
 */
@RestController
@RequestMapping("/administrativeRegion")
@RequiresPermission("administrativeRegion:manage")
public class AdministrativeRegionController {

    @Autowired
    private AdministrativeRegionService administrativeRegionService;

    @Autowired
    private LogUtils logUtils;

    @PostMapping("/create")

    public Result<Boolean> addAdministrativeRegion(@RequestBody AdministrativeRegion administrativeRegion) {
        if (administrativeRegion == null) {
            logUtils.recordLog("行政区域管理", "新增行政区域", "失败", "行政区域对象不能为空");
            return Result.error("行政区域对象不能为空");
        }
        try {
            administrativeRegion.setCreateTime(new Date());
            boolean result = administrativeRegionService.save(administrativeRegion);
            if (result) {
                // 重新获取对象以获取数据库生成的ID
                AdministrativeRegion savedRegion = administrativeRegionService.getRegionByName(administrativeRegion.getRegionName());
                logUtils.recordLog("行政区域管理", "新增行政区域", "成功",
                        "区域名称:" + savedRegion.getRegionName() + ", 区域ID:" + savedRegion.getRegionId());
                return Result.success("行政区域新增成功", true);
            } else {
                logUtils.recordLog("行政区域管理", "新增行政区域", "失败", "保存数据库失败");
                return Result.error("行政区域新增失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("行政区域管理", "新增行政区域", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete/{id}")

    public Result<Boolean> deleteAdministrativeRegion(@PathVariable Integer id) {
        if (id == null) {
            logUtils.recordLog("行政区域管理", "删除行政区域", "失败", "区域ID不能为空");
            return Result.error("区域ID不能为空");
        }
        try {
            AdministrativeRegion administrativeRegion = administrativeRegionService.getById(id);
            boolean result = administrativeRegionService.removeById(id);

            if (result) {
                logUtils.recordLog("行政区域管理", "删除行政区域", "成功",
                        "区域ID:" + id + ", 区域名称:" + (administrativeRegion != null? administrativeRegion.getRegionName() : "未知"));
                return Result.success("行政区域删除成功", true);
            } else {
                logUtils.recordLog("行政区域管理", "删除行政区域", "失败", "区域ID:" + id + "不存在");
                return Result.error("行政区域删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("行政区域管理", "删除行政区域", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")

    public Result<Boolean> updateAdministrativeRegion(@RequestBody AdministrativeRegion administrativeRegion) {
        if (administrativeRegion == null || administrativeRegion.getRegionId() == null) {
            logUtils.recordLog("行政区域管理", "更新行政区域", "失败", "行政区域对象或区域ID不能为空");
            return Result.error("行政区域对象或区域ID不能为空");
        }
        try {
            boolean result = administrativeRegionService.updateById(administrativeRegion);
            if (result) {
                logUtils.recordLog("行政区域管理", "更新行政区域", "成功",
                        "区域ID:" + administrativeRegion.getRegionId());
                return Result.success("行政区域更新成功", true);
            } else {
                logUtils.recordLog("行政区域管理", "更新行政区域", "失败",
                        "区域ID:" + administrativeRegion.getRegionId() + "不存在");
                return Result.error("行政区域更新失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("行政区域管理", "更新行政区域", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")

    public Result<AdministrativeRegion> getAdministrativeRegionById(@PathVariable Integer id) {
        if (id == null) {
            logUtils.recordLog("行政区域管理", "查询行政区域详情", "失败", "区域ID不能为空");
            return Result.error("区域ID不能为空");
        }
        try {
            AdministrativeRegion administrativeRegion = administrativeRegionService.getById(id);
            if (administrativeRegion != null) {
                /*logUtils.recordLog("行政区域管理", "查询行政区域详情", "成功", "区域ID:" + id);*/
                return Result.success("行政区域查询成功", administrativeRegion);
            } else {
                logUtils.recordLog("行政区域管理", "查询行政区域详情", "失败", "区域ID:" + id + "不存在");
                return Result.error("未找到该行政区域");
            }
        } catch (Exception e) {
            logUtils.recordLog("行政区域管理", "查询行政区域详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")

    public Result<Page<AdministrativeRegion>> getAdministrativeRegionPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String regionName) {
        if (current < 1) {
            current = 1;
        }
        if (size < 1 || size > 100) {
            size = 10;
        }
        try {
            Page<AdministrativeRegion> administrativeRegionPage = administrativeRegionService.getRegionPage(current, size, regionName);
            /*logUtils.recordLog("行政区域管理", "分页查询行政区域", "成功",
                    "页码:" + current + ", 大小:" + size + ", 区域名称:" + regionName);*/
            return Result.success("行政区域分页查询成功", administrativeRegionPage);
        } catch (Exception e) {
            logUtils.recordLog("行政区域管理", "分页查询行政区域", "失败", e.getMessage());
            return Result.error("分页查询异常: " + e.getMessage());
        }
    }

    @PostMapping("/query")

    public Result<Page<AdministrativeRegion>> queryAdministrativeRegions(
            @RequestBody AdministrativeRegion queryParam,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        if (current < 1) {
            current = 1;
        }
        if (size < 1 || size > 100) {
            size = 10;
        }
        try {
            Page<AdministrativeRegion> administrativeRegionPage = administrativeRegionService.queryRegion(queryParam, current, size);
            /*logUtils.recordLog("行政区域管理", "条件查询行政区域", "成功",
                    "条件:" + queryParam.toString() + ", 页码:" + current + ", 大小:" + size);*/
            return Result.success("行政区域条件查询成功", administrativeRegionPage);
        } catch (Exception e) {
            logUtils.recordLog("行政区域管理", "条件查询行政区域", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }
}
